package com.polaris.live.ui.message.widget.view

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.ViewTreeObserver.OnGlobalLayoutListener
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.widget.doOnTextChanged
import cn.dreamtobe.kpswitch.util.KPSwitchConflictUtil
import cn.dreamtobe.kpswitch.util.KeyboardUtil
import com.blankj.utilcode.util.ColorUtils
import com.polaris.live.R
import com.polaris.live.common.R.color
import com.polaris.live.databinding.WidgetMessageEditBinding
import com.vanniktech.emoji.EmojiTheming
import com.vanniktech.emoji.search.NoSearchEmoji

/**
 * MessageEditView
 *
 * @author Created by lucas on 2023/10/21 17:13
 */
class MessageEditView(context: Context, attributeSet: AttributeSet) :
    ConstraintLayout(context, attributeSet) {

    val mBinding = WidgetMessageEditBinding.inflate(LayoutInflater.from(context), this, true)

    private var mState: Int = STATE_NORMAL

    private var mResume = true

    private var mOnEditClickListener: OnEditClickListener? = null
    private var mOnCallVideoClickListener: OnCallVideoListener? = null
    private var mOnKeyboardListener: OnKeyboardListener? = null
    private var mOnGlobalLayoutListener: OnGlobalLayoutListener? = null
    private var mOnSelectPhotoListener: OnSelectPhotoListener? = null
    private var mKeyboardListerState: Boolean = true

    init {
        mBinding.messageEdit.doOnTextChanged { text, _, _, _ ->
            if (text.isNullOrEmpty()) {
                mBinding.sendMessage.setImageResource(R.drawable.ic_send)
                mBinding.sendMessage.isSelected = false
            } else {
                mBinding.sendMessage.setImageResource(R.drawable.ic_send)
                mBinding.sendMessage.isSelected = true
            }
        }

        mBinding.emojiView.setUp(rootView, {}, {}, mBinding.messageEdit,
            theming = EmojiTheming.from(context).copy(
                secondaryColor = ColorUtils.getColor(color.color_fe1849),
                primaryColor = ColorUtils.getColor(color.color_202028),
                backgroundColor = ColorUtils.getColor(color.white),
                textColor = ColorUtils.getColor(color.color_202028)
            ),
            searchEmoji = NoSearchEmoji
        )

        initListener()
    }

    private fun initListener() {
        mBinding.emojiShow.setOnClickListener {
            if (mState == STATE_EMOJI) {
                setState(STATE_TEXT)
            } else {
                setState(STATE_EMOJI)

                mOnKeyboardListener?.onKeyboard(true)
            }
        }
        mBinding.sendGift.setOnClickListener {
            mResume = false
            mOnEditClickListener?.onClick(it.id)
        }
        mBinding.sendMessage.setOnClickListener {
//            if (mBinding.sendMessage.isSelected) {
            mOnEditClickListener?.onClick(it.id)
//            } else {
//                mOnCallVideoClickListener?.onClick()
//            }
        }
        mBinding.selectImage.setOnClickListener {
            mOnEditClickListener?.onClick(it.id)
        }
        mBinding.selectCamera.setOnClickListener {
            mOnEditClickListener?.onClick(it.id)
        }
        mBinding.photoShow.setOnClickListener {
            mOnSelectPhotoListener?.onClick()
//            setState(STATE_MORE)
        }
    }

    fun setStateNormal() {
        setState(STATE_NORMAL)
    }

    private fun setState(state: Int) {
        if (mState == state) {
            if (state != STATE_NORMAL && state != STATE_TEXT) {
                switchKeyboard()
            }
            return
        }
        //todo 判断下自己是否被禁言

        when (mState) {
            STATE_NORMAL -> {}
            STATE_TEXT -> {}
            STATE_EMOJI -> {
                mBinding.emojiView.visibility = GONE
            }

            STATE_MORE -> {
                mBinding.llBottom.visibility = GONE
            }
        }
        when (state) {
            STATE_NORMAL -> {
                hideKeyboard()
            }

            STATE_TEXT -> {
            }

            STATE_EMOJI -> {
                mBinding.emojiView.visibility = VISIBLE
            }

            STATE_MORE -> {
                mBinding.llBottom.visibility = VISIBLE
            }
        }
        if (state != STATE_NORMAL) {
            if (state == STATE_TEXT) {
                showKeyboard()
            } else {
                showPanel(true)
            }
        }
        mState = state
    }

    private fun switchKeyboard() {
        val switchToPanel =
            KPSwitchConflictUtil.switchPanelAndKeyboard(mBinding.panel, mBinding.messageEdit)
        if (!switchToPanel) {
            mBinding.messageEdit.requestFocus()
        } else {
            mBinding.messageEdit.clearFocus()
        }
    }

    private fun showKeyboard() {
        KPSwitchConflictUtil.showKeyboard(mBinding.panel, mBinding.messageEdit)
        mBinding.messageEdit.requestFocus()
    }

    private fun showPanel(isClearFocus: Boolean) {
        KPSwitchConflictUtil.showPanel(mBinding.panel)
        if (isClearFocus) {
            mBinding.messageEdit.clearFocus()
        } else {
            mBinding.messageEdit.requestFocus()
        }
    }

    private fun hideKeyboard() {
        KPSwitchConflictUtil.hidePanelAndKeyboard(mBinding.panel)
    }

    fun setOnEditClickListener(l: OnEditClickListener) {
        mOnEditClickListener = l
    }

    fun setOnCallVideoClickListener(l: OnCallVideoListener) {
        mOnCallVideoClickListener = l
    }

    fun setOnSelectPhotoListener(l: OnSelectPhotoListener) {
        mOnSelectPhotoListener = l
    }

    fun setOnKeyboardListener(l: OnKeyboardListener) {
        mOnKeyboardListener = l
    }

    fun setKeyboardListerState(keyboardListerState: Boolean) {
        mKeyboardListerState = keyboardListerState
    }

    fun getText(): String? {
        return mBinding.messageEdit.text?.toString()
    }

    fun cleanText() {
        mBinding.messageEdit.text = null
    }

    fun setResume(resume: Boolean) {
        this.mResume = resume
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        attachKeyboard()
    }

    //附着到activity
    private fun attachKeyboard() {
        val context = context
        if (context !is Activity) {
            return
        }

        mOnGlobalLayoutListener =
            KeyboardUtil.attach(context, mBinding.panel) { isShowing: Boolean ->
                if (!mKeyboardListerState) {
                    return@attach
                }
                if (mResume) {
                    if (isShowing) {
                        setState(STATE_TEXT)
                    } else {
                        if (mState == STATE_TEXT) {
                            setState(STATE_NORMAL)
                        }
                    }

                    mOnKeyboardListener?.onKeyboard(isShowing)
                }
            }
        KPSwitchConflictUtil.attach(mBinding.panel, null, mBinding.messageEdit)
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        detachKeyboard()
    }

    private fun detachKeyboard() {
        val context = context
        if (context !is Activity) {
            return
        }
        KeyboardUtil.detach(context, mOnGlobalLayoutListener)
    }

    companion object {

        const val STATE_NORMAL = -1
        const val STATE_TEXT = 0
        const val STATE_EMOJI = 1
        const val STATE_MORE = 2

    }
}

fun interface OnEditClickListener {

    fun onClick(id: Int)

}

fun interface OnKeyboardListener {

    fun onKeyboard(isShowing: Boolean)
}

fun interface OnCallVideoListener {

    fun onClick()
}

fun interface OnSelectPhotoListener {
    fun onClick()
}
